﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Convertor.Interface;
using Wpf.CommonTools.SystemExtension;
using System.Windows;

namespace Convertor.Plugin.GenerateSequence
{
    public class Plugin : PluginBase
    {

        #region Static Func

        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.GenerateSequence;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.GenerateSequence;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #endregion

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_Array") + " - " + UILanguage.FindResource("Plugin_GenerateSequence_Name"); }
        }

        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_GenerateSequence_Desc"); }
        }


        

        

        #endregion

        #region IConvertParamters 成员

        public override IConvertParamters Output(IConvertParamters content)
        {
            if (Log != null) { Log(this, config.ToolTip, LogTypes.Normal); }

            if (string.IsNullOrEmpty(config.TemplateText) || !config.TemplateText.Contains("{0}")
                || !config.StartNumber.HasValue
                || !config.StopNumber.HasValue
                || !config.PaddingLeftNumber.HasValue)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_OpenConfigForm"), LogTypes.Normal | LogTypes.HidePluginName); }
                if (Loader.ShowDialog() == true)
                {
                    config = ((IFormGetConfigEvent)Loader.Loader).GetConfig() as ConfigContent;
                }
                else
                {
                    if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_CloseConfigForm"), LogTypes.Error | LogTypes.HidePluginName); }
                    content.StopConvert = true;
                    return content;
                }
            }

            if (string.IsNullOrEmpty(config.TemplateText))
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_GenerateSequence_Result_TemplateTextEmptry"), LogTypes.Error | LogTypes.HidePluginName); }
                content.StopConvert = true;
                return content;
            }

            if (!config.TemplateText.Contains("{0}"))
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_GenerateSequence_Result_TemplateTextNotCorrect"), LogTypes.Error | LogTypes.HidePluginName); }
                content.StopConvert = true;
                return content;
            }

            if (!config.StartNumber.HasValue || !config.StopNumber.HasValue || !config.PaddingLeftNumber.HasValue)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_GenerateSequence_Result_PrameterEmpty"), LogTypes.Error | LogTypes.HidePluginName); }
                content.StopConvert = true;
                return content;
            }

            List<string> sb = new List<string>();
            for (ulong i = config.StartNumber.Value; i <= config.StopNumber.Value; i++)
            {
                string a = string.Format(config.TemplateText, i.ToString("D" + config.PaddingLeftNumber));
                sb.Add(a);
            }
            if (Log != null) { Log(this, string.Format(UILanguage.FindResource("Plugin_GenerateSequence_ConvertSuccess"), sb.Count), LogTypes.Normal); }
            return new ConvertParamters(content.LastInputContent, sb);
        }

        #endregion

        #region IPluginConfigForm 成员

        public override PluginConfigForm ConfigForm
        {
            get { return new ConfigForm(); }
        }

        #endregion

        #region IPluginConfig 成员

        private ConfigContent config;
        public override object Config
        {
            get { return config; }
            set { config = (value as ConfigContent) ?? new ConfigContent(); }
        }

        #endregion

    }
}
